package com.lxw.library.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lxw.library.R;
import com.lxw.library.bean.ViewAttrInfo;

import java.util.List;


/**
 * 作者：李学伟
 * 日期：2018/8/10
 * 用途：
 */

public abstract class LxwBaseView<T> extends LinearLayout {
    public TextView labelTv;
    ImageView requestIv;
    public View mView;
    public Context mContext;
    TextView valueTv;
    String label;
    String paramer;
    String hint;
    int valueBg;
    int requestDrawableId;
    boolean isMust;//是否必填
    boolean showRequest;//是否显示请求按钮
    int labelColor;
    int valueColor;
    int minlines;
    int maxLine;
    int inputType;
    int maxLength;
    int orientation;
    String defaultValue;
    String submitCode;

    List<String> parentParamers;
    T dataInfo;
    ColumnRequestListener requestListener;

    public LxwBaseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.lxwBaseView);//获取配置属性
        label = tArray.getString(R.styleable.lxwBaseView_label);
        labelColor = tArray.getColor(R.styleable.lxwBaseView_labelColor, getResources().getColor(R.color.Blue));
        valueColor = tArray.getColor(R.styleable.lxwBaseView_valueColor, getResources().getColor(R.color.Black));
        minlines = tArray.getInt(R.styleable.lxwBaseView_minLine, 1);
        maxLine = tArray.getInt(R.styleable.lxwBaseView_maxLine, 3);
        inputType = tArray.getInt(R.styleable.lxwBaseView_inputType, 0);
        maxLength = tArray.getInt(R.styleable.lxwBaseView_maxLength, 0);
        orientation = tArray.getInt(R.styleable.lxwBaseView_orientation, 0);
        hint = tArray.getString(R.styleable.lxwBaseView_hint);
        defaultValue = tArray.getString(R.styleable.lxwBaseView_defaultValue);
        valueBg = tArray.getResourceId(R.styleable.lxwBaseView_valueBg, R.drawable.shape_column_bg);
        requestDrawableId = tArray.getResourceId(R.styleable.lxwBaseView_requestDrawableId, R.drawable.search);
        isMust = tArray.getBoolean(R.styleable.lxwBaseView_isMust, false);
        showRequest = tArray.getBoolean(R.styleable.lxwBaseView_showRequest, false);
        tArray.recycle();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (orientation == 0) {
            mView = inflater.inflate(getHorizontalView(), this);
        } else {
            mView = inflater.inflate(getOrientation(), this);
        }

        mContext = context;
        labelTv = mView.findViewById(R.id.labelTv);
        valueTv = mView.findViewById(R.id.valueTv);
        requestIv = mView.findViewById(R.id.requestIv);
        setAttrValue(label, labelColor, defaultValue, hint, valueColor, valueBg, isMust, minlines, maxLine, inputType);
    }

    public LxwBaseView(Context context, int orientation) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (orientation == 0) {
            mView = inflater.inflate(getHorizontalView(), this);
        } else {
            mView = inflater.inflate(getOrientation(), this);
        }
        mContext = context;
        labelTv = mView.findViewById(R.id.labelTv);
        valueTv = mView.findViewById(R.id.valueTv);
        requestIv = mView.findViewById(R.id.requestIv);
    }

    public LxwBaseView(Context context, int orientation, ColumnRequestListener listener) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (orientation == 0) {
            mView = inflater.inflate(getHorizontalView(), this);
        } else {
            mView = inflater.inflate(getOrientation(), this);
        }
        requestListener = listener;
        mContext = context;
        labelTv = mView.findViewById(R.id.labelTv);
        valueTv = mView.findViewById(R.id.valueTv);
        requestIv = mView.findViewById(R.id.requestIv);
    }

    public LxwBaseView setHint() {
        if (!TextUtils.isEmpty(hint)) {
            valueTv.setText(hint);
        }
        return this;
    }

    private void setAttrValue(String label, int labelColor, String defaultValue, String hint, int valueColor, int valueBg,
                              boolean isMust, int minlines, int maxLine, int inputType) {

        setLabel(label);
        setLabelColor(labelColor);
        setValue(defaultValue);
        if (TextUtils.isEmpty(defaultValue)) {
            setHint(hint);
        }
        setValueColor(valueColor);
        setValueBgColor(valueBg);
        setMust(isMust);
        valueTv.setMinLines(minlines);
        valueTv.setMaxLines(maxLine);
        if (maxLine == 1) {
            valueTv.setSingleLine(true);
        } else {
            valueTv.setSingleLine(false);
        }
        setInputType(inputType);
        showRequestBtn(showRequest, requestDrawableId);
    }


    public void setAttrValueByAttrInfo(ViewAttrInfo attrInfo) {
        setAttrValue(attrInfo.getLabel(), attrInfo.getLabelColor(),
                attrInfo.getDefaultValue(), attrInfo.getHint(),
                attrInfo.getValueColor(), attrInfo.getValueBg(),
                attrInfo.isMust(), attrInfo.getMinlines(), attrInfo.getMaxLine(), attrInfo.getInputType());
    }

    public abstract int getHorizontalView();

    public abstract int getVerticalView();

    public abstract <K> K getValue();

    public LxwBaseView setLabel(String label) {
        if (!TextUtils.isEmpty(label)) {
            label = label.trim();
        }
        labelTv.setText(label);
        return this;
    }

    public LxwBaseView setLabelColor(int colorId) {
        labelTv.setTextColor(getResources().getColor(colorId));
        return this;
    }

    public LxwBaseView setValueColor(int colorId) {
        valueTv.setTextColor(getResources().getColor(colorId));
        return this;
    }

    public LxwBaseView setValueBgColor(int drawable) {
        valueTv.setBackground(getResources().getDrawable(drawable));
        return this;
    }


    public boolean isMust() {
        return isMust;
    }

    public LxwBaseView setMust(boolean must) {
        isMust = must;
        if (isMust) {
            Drawable drawable = getResources().getDrawable(R.drawable.xing);
            drawable.setBounds(0, 0, 32, 32);
            labelTv.setCompoundDrawables(null, null, drawable, null);
        } else {
            labelTv.setCompoundDrawables(null, null, null, null);
        }
        return this;
    }

    public LxwBaseView setInputType(int type) {
        switch (type) {
            case 0:
                valueTv.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case 1:
                valueTv.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
                break;
            case 2:
                valueTv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case 3:
                valueTv.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                break;
            case 4:
                valueTv.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                break;
            case 5:
                valueTv.setInputType(InputType.TYPE_CLASS_PHONE);
                break;
        }
        return this;
    }

    public LxwBaseView setValue(String value) {
        if (!TextUtils.isEmpty(value)) {
            value = value.trim();
        }
        valueTv.setText(value);
        return this;
    }

    public LxwBaseView setHint(String hint) {
        valueTv.setHint(hint);
        return this;
    }

    public T getDataInfo() {
        return dataInfo;
    }

    public void setDataInfo(T dataInfo) {
        this.dataInfo = dataInfo;
    }

    public void setViewEditAble(boolean editAble) {

    }

    public String getParamer() {
        return paramer;
    }

    public String getLabel() {
        return label;
    }

    public void setViewClickAble(boolean clickAble) {
        mView.setClickable(clickAble);
    }

    public LxwBaseView showRequestBtn(boolean isShow, int resourceId) {
        if (null == requestIv) {
            requestIv = findViewById(R.id.requestIv);
        }
        if (null == requestIv) {
            return this;
        }
        if (isShow) {
            requestIv.setVisibility(View.VISIBLE);
            if (resourceId != 0) {
                requestIv.setImageResource(resourceId);
            }
            requestIv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    requestListener.requestData(paramer);
                }
            });
        } else {
            requestIv.setVisibility(View.GONE);
        }
        return this;
    }

    public  abstract boolean checkValueIsLegal();
}
